package cn.iocoder.yudao.module.trade.config;

import cn.iocoder.yudao.framework.common.enums.TerminalEnum;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.Duration;
import java.util.Map;
import java.util.Objects;

/**
 * @author cai
 * @date 2024/04/03 14:09
 */
@Component
@ConfigurationProperties(prefix = "trade.order.limit")
@Data
public class TradeOrderLimitConfig {

    private Map<String, TerminalConfig> terminalConfig;

    @Data
    public static class TerminalConfig {
        private Boolean enable = false;

        private String currency = "CNY";

        private BigDecimal dayAmount = BigDecimal.valueOf(3000);

        private BigDecimal monthAmount = BigDecimal.valueOf(30000);
    }

    public TerminalConfig defaultConfig() {
        TerminalConfig config = terminalConfig.get("DEFAULT");
        if (config == null) {
            config = new TerminalConfig();
            config.setEnable(true);
        }
        return config;
    }

    public String getCurrency(Integer terminal) {
        TradeOrderLimitConfig.TerminalConfig config = defaultConfig();
        if (Objects.nonNull(terminal)) {
            TerminalEnum terminalEnum = TerminalEnum.fromValue(terminal);
            Map<String, TradeOrderLimitConfig.TerminalConfig> terminalConfig = getTerminalConfig();
            config = terminalConfig.get(terminalEnum.name());
            if (Objects.isNull(config)) {
                config = defaultConfig();
            }
        }
        return config.getCurrency();
    }

}
